package com.webchat.client.payment.service;


import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.common.exception.BusinessException;
import com.webchat.domain.vo.request.SendRedPacketRequestVO;
import com.webchat.rmi.ugc.RedPacketClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RedPacketService {

    /**
     * 注入 UGC 远程服务的红包服务客户端
     */
    @Autowired
    private RedPacketClient redPacketClient;

    /**
     * 发红包
     *
     * CLIENT -> UGC -> PAY 服务调用连
     *
     * @param sendRedPacketRequest
     * @return
     */
    public Long sendRedPacket(SendRedPacketRequestVO sendRedPacketRequest) {

        // RPC远程调用，红包实际的收发逻辑由UGC实现，底层钱包扣除由PAY服务支持
        APIResponseBean<Long> responseBean = redPacketClient.send(sendRedPacketRequest);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        throw new BusinessException(responseBean.getMsg());
    }

    /**
     * 拆红包
     *
     * CLIENT -> UGC -> PAY 服务调用连
     *
     * @param redPacketId 红包id
     * @param userId 拆包用户id
     * @return
     */
    public String openRedPacket(Long redPacketId, String userId) {

        // RPC远程调用，红包实际的收发逻辑由UGC实现，底层钱包扣除由PAY服务支持
        APIResponseBean<String> responseBean = redPacketClient.open(redPacketId, userId);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        throw new BusinessException(responseBean.getMsg());
    }
}
